<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>聊天室</title>
  </head>
  <body>
    <h1>聊天室</h1>
    <button class="btn">发送消息</button>
    <script src="/socket.io/socket.io.js"></script>
    <script>
      // 客户端连接服务器聊天室
      const socket = io(); // socket丧气用户端对象

      socket.on("bbb", (content) => {
        console.log("服务器给客户端发送消息", content);
      });

      document.querySelector(".btn").onclick = function () {
        // 客户端给服务器发送消息
        socket.emit("ccc", {
          time: Date.now(),
          content: "hello~~",
        });
      };
    </script>
  </body>
</html>
